package com.coollang.smartbra.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.coollang.smartbra.MyAutoLayoutActivity;
import com.coollang.smartbra.R;
import com.coollang.smartbra.dialog.ShareBitmapDialog;
import com.coollang.smartbra.model.ActionDataModel;
import com.coollang.smartbra.presenter.ShareActivityPresenter;
import com.coollang.smartbra.utils.ShareUtils;
import com.coollang.smartbra.utils.TVDrawableUtils;
import com.coollang.smartbra.utils.TimeUtils;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;
import com.umeng.socialize.UMShareAPI;

import butterknife.BindView;
import butterknife.ButterKnife;
import eightbitlab.com.blurview.BlurView;
import eightbitlab.com.blurview.RenderScriptBlur;

/**
 * 运动结束后页面
 */

public class ShareActivity extends MyAutoLayoutActivity implements View.OnClickListener {

    private static final String TAG = "ShareActivity";
    @BindView(R.id.blurView)
    BlurView blurView;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_sport_finish)
    TextView tvSportFinish;
    @BindView(R.id.tv_hit)
    TextView tvHit;
    @BindView(R.id.share)
    TextView share;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.status_bar)
    View statusBar;
    @BindView(R.id.content)
    RelativeLayout content;

    int mType;
    @BindView(R.id.tv_finished)
    TextView tvFinished;

    final String[] actionName = {"", "深蹲", "深蹲跳", "仰卧起坐", "卷腹", "俯卧撑", "平板支撑"};

    private static final int[] uMoney = {0, 30, 45, 55, 60, 70, 60};
    private SpannableString msp;
    private ShareActivityPresenter mPresenter;
    private ShareUtils mShareUtils;
    private ShareBitmapDialog mShareBitmapDialog;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_share);
        ButterKnife.bind(this);
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(statusBar, this, content);
        initBlur();
        initBuandle();
        initListener();
        setDisPlay();
    }

    private void setDisPlay() {
        switch (mType) {
            case 1:
                TVDrawableUtils.drawRight(tvSportFinish, R.drawable.icon_sd);
                break;
            case 2:
                TVDrawableUtils.drawRight(tvSportFinish, R.drawable.icon_sdt);
                break;
            case 3:
                TVDrawableUtils.drawRight(tvSportFinish, R.drawable.icon_ywqz);
                break;
            case 4:
                TVDrawableUtils.drawRight(tvSportFinish, R.drawable.icon_jf);
                break;
            case 5:
                TVDrawableUtils.drawRight(tvSportFinish, R.drawable.icon_fwc);
                break;
            case 6:
                TVDrawableUtils.drawRight(tvSportFinish, R.drawable.icon_pbzc);
                break;
            default:
                break;
        }
    }


    private void initBuandle() {
        Intent intent = getIntent();
        mType = intent.getIntExtra("type", 1);
    }

    private void initBlur() {
        final float radius = 16f;
        final View decorView = getWindow().getDecorView();
        final View rootView = decorView.findViewById(android.R.id.content);
        final Drawable windowBackground = decorView.getBackground();
        blurView.setupWith(rootView)
                .windowBackground(windowBackground)
                .blurAlgorithm(new RenderScriptBlur(this, true))
                .blurRadius(radius);
    }

    private void initListener() {
        ivClose.setOnClickListener(this);
        share.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        mPresenter = new ShareActivityPresenter(this);

        // 查询数据
        ActionDataModel model = mPresenter.queryNumberActionData();

        // 展示数据,并且上传数据
        long nums = model.getNums();

        if (mType != 6) {
            if (mType == 3) {
                setNumberUI(nums, 5);
            } else {
                setNumberUI(nums, 4);
            }
        } else {
            setPbzcUI(nums);
        }

        mPresenter.uploadData(model);
    }

    private void setNumberUI(long nums, int length) {

        String text = "本次共完成 " + nums + " 个" + actionName[mType];
        msp = new SpannableString(text);
        // 设置字体大小
        msp.setSpan(new RelativeSizeSpan(1.5f), 6, text.length() - length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        // 设置字体颜色
        msp.setSpan(new ForegroundColorSpan(0xFFFCC707), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new ForegroundColorSpan(0xFFF79444), 6, text.length() - length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        tvFinished.setText(msp);

        setUMoney(nums);
    }

    private void setPbzcUI(long nums) {
        String time = TimeUtils.getFormatmillisecond(nums);
        String text = "本次平板支撑坚持了 " + time;
        msp = new SpannableString(text);
        // 设置字体大小
        msp.setSpan(new RelativeSizeSpan(1.5f), text.length() - 8, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 设置字体颜色
        msp.setSpan(new ForegroundColorSpan(0xFFFCC707), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new ForegroundColorSpan(0xFFF79444), text.length() - 8, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        tvFinished.setText(msp);

        setUMoney(nums);
    }

    private void setUMoney(long nums) {

        long uMoneyCount;

        if (mType != 6) {
            uMoneyCount = nums * uMoney[mType];
        } else {
            uMoneyCount = (int) (nums / 10000) * uMoney[mType];
        }

        String uMoney = "获得 " + uMoneyCount + " U币";
        msp = new SpannableString(uMoney);

        // 设置字体大小
        msp.setSpan(new RelativeSizeSpan(1.5f), 2, uMoney.length() - 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        // 设置字体颜色
        msp.setSpan(new ForegroundColorSpan(0xFFFCC707), 0, uMoney.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new ForegroundColorSpan(0xFFF79444), 3, uMoney.length() - 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        tvHit.setText(msp);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_close:
                finish();
                break;
            case R.id.share:

                if (mShareUtils == null) {
                    mShareUtils = new ShareUtils(this);
                    mShareUtils.initShare();
                    mShareBitmapDialog = new ShareBitmapDialog(this, null);
                    mShareBitmapDialog.setOnclickListener(mOnclickListener);
                }

                mShareBitmapDialog.show();
                break;
            default:
                break;
        }
    }

    // 分享=============================
    private ShareBitmapDialog.OnclickListener mOnclickListener = new ShareBitmapDialog.OnclickListener() {
        @Override
        public void onCancel() {
            mShareBitmapDialog.dismiss();
        }

        @Override
        public void onWeixin() {
            mShareUtils.share(1);
        }

        @Override
        public void onWEXIN_CIRCLE() {
            mShareUtils.share(2);
        }

        @Override
        public void onWeibo() {
            mShareUtils.share(3);
        }

        @Override
        public void onQQ() {
            mShareUtils.share(4);
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        UMShareAPI.get(this).release();
    }
}
